package com.base.controller.api;

import com.base.helper.ApiinfoesHelper;
import com.base.po.param.NetState;
import com.base.po.sqlparam.UsersExamplePage;
import com.base.po.voip.*;
import com.base.service.*;
import com.base.utils.GsonUtil;
import com.base.utils.Produce;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("app")
public class AppInfoController {

	private final SubscribersService subscribersService;

	private final UsersService usersService;

	private final AdversimentsService adversimentsService;

	private final AgentinfoesService agentinfoesService;

	public AppInfoController(SubscribersService subscribersService,
							 UsersService usersService,
							 AdversimentsService adversimentsService,
							 AgentinfoesService agentinfoesService) {
		this.subscribersService = subscribersService;
		this.usersService = usersService;
		this.adversimentsService = adversimentsService;
		this.agentinfoesService = agentinfoesService;
	}

	// 启动图
	@RequestMapping(value = "/homePage", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody NetState getHomePage(HttpServletRequest request, String Mobile, String ParentId)
			throws Exception {
		// 返回数据对象
		NetState netState = new NetState();
		AdversimentsExample adversimentsExample = new AdversimentsExample();
		Object jsonDefault = null;

		// 代理商信息
		Users pUser = null;

		// 追加内容 如果ADMIN添加了紧急控制图片 则显示ADMIN的图片
		AgentinfoesExample AgentinfoesExample = new AgentinfoesExample();
		AgentinfoesExample.createCriteria().andAgentidEqualTo("7dd9a07e-9cef-4702-a165-b7033861c1b7")
				.andDatastatusEqualTo(true).andHitsEqualTo(1); // Hits=1 是启动页
		List<Agentinfoes> agentinfoes = agentinfoesService.selectByExample(AgentinfoesExample);
		if (agentinfoes != null && agentinfoes.size() != 0) {
			Adversiments adversiments = new Adversiments(agentinfoes.get(0).getId(), agentinfoes.get(0).getAgentid(),
					agentinfoes.get(0).getHits(), agentinfoes.get(0).getUrl(), 0, 0, agentinfoes.get(0).getDatastatus(),
					agentinfoes.get(0).getLogo(), Produce.getNowDate());
			netState.setJson(adversiments);
			netState.setOtherJson(pUser);
			netState.setData("紧急控制启动图");
			netState.setErrorCode(NetState.SUCCESS_CONN);
		} else {
			// 获取系统默认启动图
			List<Adversiments> adversiments;
			adversimentsExample.createCriteria().andParentidEqualTo("7DD9A07E-9CEF-4702-A165-B7033861C1B7")
					.andAdtypeEqualTo(1).andDatastatusEqualTo(true);
			adversimentsExample.setOrderByClause("CreateTime DESC");
			adversiments = adversimentsService.selectByExample(adversimentsExample);
			if (adversiments != null && adversiments.size() > 0) {
				if (adversiments.get(0).getPath() != null || !adversiments.get(0).getPath().trim().equals("")) {
					jsonDefault = adversiments.get(0);
				} else {
					jsonDefault = "pic is null";
				}
			} else {
				jsonDefault = "pic is null";
			}
			// 空参数获取系统启动图片
			if (Mobile == null || Mobile.equals("") && ParentId == null || ParentId.equals("")) {
				netState.setJson(jsonDefault);
				netState.setOtherJson("参数为空");
				netState.setData("系统默认启动页");
				netState.setErrorCode(NetState.SUCCESS_CONN);
			} else {
				// 代理商信息
				pUser = usersService.selectByPrimaryKey(ParentId);
				// 查询用户信息，如果该用户有商家的就用商家的id查询，
				// 如果没有商家的就用代理id查询，如果代理id没有内容就用默认的
				SubscribersExample subscribersExample = new SubscribersExample();
				subscribersExample.getOredCriteria().add(subscribersExample.createCriteria().andMobileEqualTo(Mobile)
						.andParentidEqualTo(ParentId).andDatastatusEqualTo(true));
				List<Subscribers> subscribers = subscribersService.selectByExample(subscribersExample);
				if (subscribers != null && subscribers.size() > 0) {
					Subscribers sub = subscribers.get(0);// 当前用户
					// 用户有商家的时候
					if (sub.getShopid() != null) {
						adversimentsExample.clear();
						adversimentsExample.getOredCriteria().add(adversimentsExample.createCriteria()
								.andParentidEqualTo(sub.getShopid()).andAdtypeEqualTo(1).andDatastatusEqualTo(true));
						adversimentsExample.setOrderByClause("CreateTime DESC");
						adversiments = adversimentsService.selectByExample(adversimentsExample);
						if (adversiments != null && adversiments.size() > 0) {
							// 商家有图
							if (adversiments.get(0).getPath() != null
									|| !adversiments.get(0).getPath().trim().equals("")) {
								netState.setJson(adversiments.get(0));
								netState.setOtherJson(pUser);
								netState.setData("商家自定义启动页");
								netState.setErrorCode(NetState.SUCCESS_CONN);
							} else {
								netState.setJson("pic is null");
								netState.setOtherJson(pUser);
								netState.setData("无图");
								netState.setErrorCode(NetState.NODATA);
							}
						} else {
							netState.setJson("pic is null");
							netState.setOtherJson(pUser);
							netState.setData("无图");
							netState.setErrorCode(NetState.NODATA);
						}
						// 商家没图，用代理的图
						if (netState.getJson().equals("pic is null")) {
							adversimentsExample.clear();
							adversimentsExample.getOredCriteria()
									.add(adversimentsExample.createCriteria().andParentidEqualTo(sub.getParentid())
											.andAdtypeEqualTo(1).andDatastatusEqualTo(true));
							adversimentsExample.setOrderByClause("CreateTime DESC");
							adversiments = adversimentsService.selectByExample(adversimentsExample);
							if (adversiments != null && adversiments.size() > 0) {
								if (adversiments.get(0).getPath() != null
										|| !adversiments.get(0).getPath().trim().equals("")) {
									netState.setData("代理商自定义启动页");
									netState.setJson(adversiments.get(0));
									netState.setOtherJson(pUser);
									netState.setErrorCode(NetState.SUCCESS_CONN);
								} else {
									netState.setJson("pic is null");
									netState.setData("无图");
									netState.setOtherJson(pUser);
									netState.setErrorCode(NetState.NODATA);
								}
							} else {
								netState.setJson("pic is null");
								netState.setData("无图");
								netState.setOtherJson(pUser);
								netState.setErrorCode(NetState.NODATA);
							}
						}
					}
					// 代理商家都没有图
					if (netState.getJson().equals("pic is null")) {
						netState.setData("系统默认启动页");
						netState.setJson(jsonDefault);
						netState.setOtherJson(pUser);
						netState.setErrorCode(NetState.SUCCESS_CONN);
					}
				} else {
					// 用户找不到，直接用代理的图
					adversimentsExample.clear();
					adversimentsExample.getOredCriteria().add(adversimentsExample.createCriteria()
							.andParentidEqualTo(ParentId).andAdtypeEqualTo(1).andDatastatusEqualTo(true));
					adversimentsExample.setOrderByClause("CreateTime DESC");
					adversiments = adversimentsService.selectByExample(adversimentsExample);
					if (adversiments != null && adversiments.size() > 0) {
						if (adversiments.get(0).getPath() != null || !adversiments.get(0).getPath().trim().equals("")) {
							netState.setJson(adversiments.get(0));
							netState.setOtherJson(pUser);
							netState.setData("代理商自定义启动图");
							netState.setErrorCode(NetState.SUCCESS_CONN);
						} else {
							netState.setJson("pic is null");
							netState.setOtherJson(pUser);
							netState.setData("无图");
							netState.setErrorCode(NetState.NODATA);
						}
					} else {
						netState.setJson("pic is null");
						netState.setOtherJson(pUser);
						netState.setData("无图");
						netState.setErrorCode(NetState.NODATA);
					}
					// 代理没有图
					if (netState.getJson().equals("pic is null")) {
						netState.setJson(jsonDefault);
						netState.setOtherJson(pUser);
						netState.setData("系统默认启动广告");
						netState.setErrorCode(NetState.SUCCESS_CONN);
					}
				}
			}
		}
//		Apiinfoes apiinfoes = ApiinfoesHelper.createApiinfoes(request, "获取启动图片");
//		apiinfoesService.insert(ApiinfoesHelper.setApiinfoes(apiinfoes, Produce.parmsToString(ParentId, Mobile),
//				GsonUtil.createJsonString(netState)));
		return netState;
	}

	// 获取所有banner页面
	@RequestMapping(value = "/homeBanner", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody NetState getHomeBanner(HttpServletRequest request, String Mobile, String ParentId)
			throws Exception {
		// 返回数据对象
		NetState netState = new NetState();
		AdversimentsExample adversimentsExample = new AdversimentsExample();
		Object jsonDefault = null;

		// 代理商信息
		Users pUser = null;

		// 获取系统默认启动图
		List<Adversiments> adversiments = new ArrayList<Adversiments>();
		// 如果ADMIN添加了紧急控制图片 则追加ADMIN的图片
		List<Adversiments> addAdversiments = new ArrayList<Adversiments>(); // 存放追加信息
		AgentinfoesExample AgentinfoesExample = new AgentinfoesExample();
		AgentinfoesExample.createCriteria().andAgentidEqualTo("7dd9a07e-9cef-4702-a165-b7033861c1b7")
				.andDatastatusEqualTo(true).andHitsGreaterThanOrEqualTo(1).andHitsLessThanOrEqualTo(4); // Hits=1234
		List<Agentinfoes> agentinfoes = agentinfoesService.selectByExample(AgentinfoesExample);
		if (agentinfoes != null && agentinfoes.size() != 0) {
			for (Agentinfoes age : agentinfoes) {
				addAdversiments.add(new Adversiments(age.getId(), age.getAgentid(), age.getHits(), age.getUrl(),
						0, 0, age.getDatastatus(), age.getLogo(), Produce.getNowDate()));
			}
		}
		// 原内容
		adversimentsExample.getOredCriteria().add(adversimentsExample.createCriteria()
				.andParentidEqualTo("7DD9A07E-9CEF-4702-A165-B7033861C1B7").andDatastatusEqualTo(true));
		adversimentsExample.setOrderByClause("CreateTime DESC");
		adversiments = adversimentsService.selectByExample(adversimentsExample);
		if (adversiments != null && adversiments.size() > 0) {
			// 追加信息
			adversiments.addAll(addAdversiments);
			jsonDefault = adversiments;
		} else {
			jsonDefault = "pic is null";
		}
		// 空参数获取系统banner图片
		if (Mobile == null || Mobile.equals("") && ParentId == null || ParentId.equals("")) {
			netState.setJson(jsonDefault);
			netState.setOtherJson("参数为空");
			netState.setData("系统默认banner广告");
			netState.setErrorCode(NetState.SUCCESS_CONN);
		} else {
			// 代理商信息
			pUser = usersService.selectByPrimaryKey(ParentId);
			// 查询用户信息，如果该用户有商家的就用商家的id查询，
			// 如果没有商家的就用代理id查询，如果代理id没有内容就用默认的
			SubscribersExample subscribersExample = new SubscribersExample();
			subscribersExample.getOredCriteria().add(subscribersExample.createCriteria().andMobileEqualTo(Mobile)
					.andParentidEqualTo(ParentId).andDatastatusEqualTo(true));
			List<Subscribers> subscribers = subscribersService.selectByExample(subscribersExample);
			if (subscribers != null && subscribers.size() > 0) {
				Subscribers sub = subscribers.get(0);// 当前用户
				// 用户有商家的时候
				if (sub.getShopid() != null) {
					adversimentsExample.clear();
					adversimentsExample.getOredCriteria().add(adversimentsExample.createCriteria()
							.andParentidEqualTo(sub.getShopid()).andDatastatusEqualTo(true));
					adversimentsExample.setOrderByClause("CreateTime DESC");
					adversiments = adversimentsService.selectByExample(adversimentsExample);
					if (adversiments != null && adversiments.size() > 0) {
						// 追加信息
						adversiments.addAll(addAdversiments);
						// 商家有图
						netState.setJson(adversiments);
						netState.setOtherJson(pUser);
						netState.setData("商家自定义banner图");
						netState.setErrorCode(NetState.SUCCESS_CONN);
					} else {
						netState.setJson("pic is null");
						netState.setOtherJson(pUser);
						netState.setData("无图");
						netState.setErrorCode(NetState.NODATA);
					}
					// 商家没图，用代理的图
					if (netState.getJson().equals("pic is null")) {
						adversimentsExample.clear();
						adversimentsExample.getOredCriteria().add(adversimentsExample.createCriteria()
								.andParentidEqualTo(sub.getParentid()).andDatastatusEqualTo(true));
						adversimentsExample.setOrderByClause("CreateTime DESC");
						adversiments = adversimentsService.selectByExample(adversimentsExample);
						if (adversiments != null && adversiments.size() > 0) {
							// 追加信息
							adversiments.addAll(addAdversiments);
							netState.setData("代理商自定义启动页");
							netState.setJson(adversiments);
							netState.setOtherJson(pUser);
							netState.setErrorCode(NetState.SUCCESS_CONN);
						} else {
							netState.setJson("pic is null");
							netState.setOtherJson(pUser);
							netState.setData("无图");
							netState.setErrorCode(NetState.NODATA);
						}
					}
				}
				// 代理商家都没有图
				if (netState.getJson().equals("pic is null")) {
					netState.setData("系统默认启动页");
					netState.setJson(jsonDefault);
					netState.setOtherJson(pUser);
					netState.setErrorCode(NetState.SUCCESS_CONN);
				}
			} else {
				// 用户找不到，直接用代理的图
				adversimentsExample.clear();
				adversimentsExample.getOredCriteria().add(
						adversimentsExample.createCriteria().andParentidEqualTo(ParentId).andDatastatusEqualTo(true));
				adversimentsExample.setOrderByClause("CreateTime DESC");
				adversiments = adversimentsService.selectByExample(adversimentsExample);
				if (adversiments != null && adversiments.size() > 0) {
					// 追加信息
					adversiments.addAll(addAdversiments);
					netState.setData("代理商自定义启动页");
					netState.setJson(adversiments);
					netState.setOtherJson(pUser);
					netState.setErrorCode(NetState.SUCCESS_CONN);
				} else {
					netState.setJson("pic is null");
					netState.setOtherJson(pUser);
					netState.setData("无图");
					netState.setErrorCode(NetState.NODATA);
				}
				// 代理没有图
				if (netState.getJson().equals("pic is null")) {
					netState.setJson(jsonDefault);
					netState.setOtherJson(pUser);
					netState.setData("系统默认banner广告");
					netState.setErrorCode(NetState.SUCCESS_CONN);
				}
			}
		}
//		Apiinfoes apiinfoes = ApiinfoesHelper.createApiinfoes(request, "获取所有Banner图片");
//		apiinfoesService.insert(ApiinfoesHelper.setApiinfoes(apiinfoes, Produce.parmsToString(ParentId, Mobile),
//				GsonUtil.createJsonString(netState)));
		return netState;
	}

	// 微信banner图  暂时只传 键盘页图片
	@CrossOrigin
	@RequestMapping(value = "/wxHomeBanner", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody NetState wxHomeBanner(HttpServletRequest request, String Mobile, String ParentId)
			throws Exception {
		// 返回数据对象
		NetState netState = new NetState();
		AdversimentsExample adversimentsExample = new AdversimentsExample();
		Object jsonDefault = null;
		// 获取系统默认启动图
		List<Adversiments> adversiments = new ArrayList<Adversiments>();
		// 如果ADMIN添加了紧急控制图片 则追加ADMIN的图片
		List<Adversiments> addAdversiments = new ArrayList<Adversiments>(); // 存放追加信息
		AgentinfoesExample AgentinfoesExample = new AgentinfoesExample();
		AgentinfoesExample.createCriteria().andAgentidEqualTo("7dd9a07e-9cef-4702-a165-b7033861c1b7")
				.andDatastatusEqualTo(true).andHitsGreaterThanOrEqualTo(1).andHitsEqualTo(3);
		List<Agentinfoes> agentinfoes = agentinfoesService.selectByExample(AgentinfoesExample);
		if (agentinfoes != null && agentinfoes.size() != 0) {
			for (Agentinfoes age : agentinfoes) {
				addAdversiments.add(new Adversiments(age.getId(), age.getAgentid(), age.getHits(), age.getUrl(),
						0, 0, age.getDatastatus(), age.getLogo(), Produce.getNowDate()));
			}
		}
		// 原内容
		adversimentsExample.getOredCriteria().add(adversimentsExample.createCriteria()
				.andParentidEqualTo("7DD9A07E-9CEF-4702-A165-B7033861C1B7").andDatastatusEqualTo(true));
		adversimentsExample.setOrderByClause("CreateTime DESC");
		adversiments = adversimentsService.selectByExample(adversimentsExample);
		if (adversiments != null && adversiments.size() > 0) {
			// 追加信息
			adversiments.addAll(addAdversiments);
			jsonDefault = adversiments;
		} else {
			jsonDefault = "pic is null";
//			jsonDefault = null;
		}
		// 空参数获取系统banner图片
		if (Mobile == null || Mobile.equals("") && ParentId == null || ParentId.equals("")) {
			netState.setJson(jsonDefault);
			netState.setData("系统默认banner广告");
			netState.setErrorCode(NetState.SUCCESS_CONN);
		} else {
			// 查询用户信息，如果该用户有商家的就用商家的id查询，
			// 如果没有商家的就用代理id查询，如果代理id没有内容就用默认的
			SubscribersExample subscribersExample = new SubscribersExample();
			subscribersExample.getOredCriteria().add(subscribersExample.createCriteria().andMobileEqualTo(Mobile)
					.andParentidEqualTo(ParentId).andDatastatusEqualTo(true));
			List<Subscribers> subscribers = subscribersService.selectByExample(subscribersExample);
			if (subscribers != null && subscribers.size() > 0) {
				Subscribers sub = subscribers.get(0);// 当前用户
				// 用户有商家的时候
				if (sub.getShopid() != null) {
					adversimentsExample.clear();
					adversimentsExample.getOredCriteria().add(adversimentsExample.createCriteria()
							.andParentidEqualTo(sub.getShopid()).andDatastatusEqualTo(true));
					adversimentsExample.setOrderByClause("CreateTime DESC");
					adversiments = adversimentsService.selectByExample(adversimentsExample);
					if (adversiments != null && adversiments.size() > 0) {
						// 追加信息
						adversiments.addAll(addAdversiments);
						// 商家有图
						netState.setJson(adversiments);
						netState.setData("商家自定义banner图");
						netState.setErrorCode(NetState.SUCCESS_CONN);
					} else {
						netState.setJson("pic is null");
						netState.setData("无图");
						netState.setErrorCode(NetState.NODATA);
					}
					// 商家没图，用代理的图
					if (netState.getJson().equals("pic is null")) {
						adversimentsExample.clear();
						adversimentsExample.getOredCriteria().add(adversimentsExample.createCriteria()
								.andParentidEqualTo(sub.getParentid()).andDatastatusEqualTo(true));
						adversimentsExample.setOrderByClause("CreateTime DESC");
						adversiments = adversimentsService.selectByExample(adversimentsExample);
						if (adversiments != null && adversiments.size() > 0) {
							// 追加信息
							adversiments.addAll(addAdversiments);
							netState.setData("代理商自定义启动页");
							netState.setJson(adversiments);
							netState.setErrorCode(NetState.SUCCESS_CONN);
						} else {
							netState.setJson("pic is null");
							netState.setData("无图");
							netState.setErrorCode(NetState.NODATA);
						}
					}
				}
				// 代理商家都没有图
				if (netState.getJson().equals("pic is null")) {
					netState.setData("系统默认启动页");
					netState.setJson(jsonDefault);
					netState.setErrorCode(NetState.SUCCESS_CONN);
				}
			} else {
				// 用户找不到，直接用代理的图
				adversimentsExample.clear();
				adversimentsExample.getOredCriteria().add(
						adversimentsExample.createCriteria().andParentidEqualTo(ParentId).andDatastatusEqualTo(true));
				adversimentsExample.setOrderByClause("CreateTime DESC");
				adversiments = adversimentsService.selectByExample(adversimentsExample);
				if (adversiments != null && adversiments.size() > 0) {
					// 追加信息
					adversiments.addAll(addAdversiments);
					netState.setData("代理商自定义启动页");
					netState.setJson(adversiments);
					netState.setErrorCode(NetState.SUCCESS_CONN);
				} else {
					netState.setJson("pic is null");
					netState.setData("无图");
					netState.setErrorCode(NetState.NODATA);
				}
				// 代理没有图
				if (netState.getJson().equals("pic is null")) {
					netState.setJson(jsonDefault);
					netState.setData("系统默认banner广告");
					netState.setErrorCode(NetState.SUCCESS_CONN);
				}
			}
		}
//		Apiinfoes apiinfoes = ApiinfoesHelper.createApiinfoes(request, "获取所有Banner图片");
//		apiinfoesService.insert(ApiinfoesHelper.setApiinfoes(apiinfoes, Produce.parmsToString(ParentId, Mobile),
//				GsonUtil.createJsonString(netState)));
		return netState;
	}

	// 更新版本
	@RequestMapping(value = "/updateVersion", method = { RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody Map<String, Object> updateVersion(HttpServletRequest request, String ParentId) throws Exception {
		Map<String, Object> netState = new HashMap<>();
		if (ParentId == null || ParentId.equals("")) {
			netState.put("errorCode", NetState.PARAMS_ERROR);
			netState.put("data", "客户端参数错误");
		} else {
			UsersExample usersExample = new UsersExamplePage();
			usersExample.getOredCriteria()
					.add(usersExample.createCriteria().andIdEqualTo(ParentId).andDatastatusEqualTo(true));
			List<Users> users = usersService.selectByExample(usersExample);
			if (users != null && users.size() == 1) {
				Users agent = users.get(0);
//				Users result = new Users();
//				result.setId(agent.getId());
//				result.setAndroidversion(agent.getAndroidversion());
//				result.setAndroidpath(agent.getAndroidpath());
//				result.setIosversion(agent.getIosversion());
//				result.setIospath(agent.getIospath());

				Map<String, Object> result = new HashMap<>();
				result.put("id", agent.getId());
				result.put("androidversion", agent.getAndroidversion());
				result.put("androidpath", agent.getAndroidpath());
				result.put("iosversion", agent.getIosversion());
				result.put("iospath", agent.getIospath());
				Map<String, Object> iosMap = new HashMap<>();
				iosMap.put("IOSVersion", agent.getIosversion());
				iosMap.put("IOSPath", agent.getIospath());
				netState.put("errorCode", NetState.SUCCESS_CONN);
				netState.put("data", "成功找到该代理商");
				netState.put("json", result);
				netState.put("Info", iosMap);
			} else {
				netState.put("errorCode", NetState.NODATA);
				netState.put("data", "没能找到该代理商");
			}
		}
//		Apiinfoes apiinfoes = ApiinfoesHelper.createApiinfoes(request, "更新版本");
//		apiinfoesService.insert(ApiinfoesHelper.setApiinfoes(apiinfoes, Produce.parmsToString(ParentId),
//				GsonUtil.createJsonString(netState)));
		return netState;
	}
}
